package action.borrowinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import bizlogic.bookinfo.BookInfoService;
import bizlogic.bookinfo.domain.BookInfo;
import bizlogic.borrowinfo.BorrowInfoService;
import bizlogic.borrowinfo.domain.BorrowInfo;

@WebServlet("/borrowinfolistaction")
public class BorrowinfoListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    BorrowInfoService borrowInfoService=new BorrowInfoService();   

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if("queryBorrowInfo".equals(request.getParameter("method"))) {
			queryBorrowInfo(request, response);
		}
		if("deleteBorrowInfo".equals(request.getParameter("method"))) {
			deleteBorrowInfo(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/*
     * 功能需求：能够根据id值调用service相关方法删除数据；
     */
	public void deleteBorrowInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String borrowId=request.getParameter("borrowid");
		String string = borrowInfoService.deleteBorrowInfoByBorrowId(borrowId);
		response.getWriter().write(string);
	}

	/*
     * 功能需求：能够根据pageIndex、pageSize、bookName、bookType查询图书信息
     */
	public void queryBorrowInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize=Integer.parseInt(request.getParameter("pageSize"));
		String sortField=request.getParameter("sortField");
		String sortOrder=request.getParameter("sortOrder");
		//每张分页数据,条数固定10条
		List<BorrowInfo> queryBorrowInfoByKeyWords = borrowInfoService.queryBorrowInfoByKeyWords(pageIndex, pageSize, sortField, sortOrder);
		List<Map<String, Object>> borrowinfoMap=new ArrayList<Map<String,Object>>();
		//利用map充当临时实体
		for(BorrowInfo borrowInfo:queryBorrowInfoByKeyWords) {
			Map<String, Object> map=new HashMap<>();
			map.put("borrowid",borrowInfo.getBorrowid() );
			map.put("bookid",borrowInfo.getBookid() );
			map.put("borrower",borrowInfo.getBorrower() );
			map.put("phone",borrowInfo.getPhone() );
			map.put("borrowtime",borrowInfo.getBorrowtime() );
			map.put("returntime", borrowInfo.getReturntime());
			map.put("bookname", getBookName(borrowInfo.getBookid()));
			borrowinfoMap.add(map);
		}
		//获取总条数
		int allBorrowinfoNum=borrowInfoService.findLength();
		//复合条件条数
		int Totalsize = borrowInfoService.queryBorrowInfoByKeyWords(0, allBorrowinfoNum, null, "").size();
		
		
		HashMap<String, Object> hashMap = new HashMap<>();
		hashMap.put("data", borrowinfoMap);
		hashMap.put("total", Totalsize);
		response.getWriter().write(JSONObject.toJSONStringWithDateFormat(hashMap, "yyyy-MM-dd HH:mm:ss"));
	}

	/*
     * 将bookid转化成bookname
     */
	private String getBookName(String bookid) {
		BookInfoService bookInfoService=new BookInfoService();
		BookInfo queryBookInfoByID = bookInfoService.queryBookInfoByID(bookid);
		return queryBookInfoByID==null?"":queryBookInfoByID.getBookname();
	}

}
